package com.example.my_objectives.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Unit extends Database {

	private final String		LOG	= Unit.class.getName();

	private long				_id;
	private String				_name;

	public Unit(SQLiteDatabase db) {
		super(db);
		set_database(db);
	}

	public long getId() {
		return _id;
	}

	public void setId(long id) {
		this._id = id;
	}

	public String getName() {
		return _name;
	}

	public void setName(String name) {
		this._name = name;
	}

	@Override
	public Exception createTable() {
		Exception returnValue = null;
		try {
			String query = "CREATE TABLE " + TableName.unit.toString() + "(id INTEGER, name TEXT, "
				+ "CONSTRAINT 'unit_pk' PRIMARY KEY(id))";
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	public Exception defaultInserts() {
		Exception returnValue = null;
		try {
			Log.d(LOG, "*** Table inserts ***");
			String query = "INSERT INTO " + TableName.unit.toString() + " (`id`, `name`) " + "VALUES (1, 'time(s)')";
			Log.d(LOG, query);
			get_database().execSQL(query);

			query = "INSERT INTO " + TableName.unit.toString() + " (`id`, `name`) " + "VALUES (2, 'page(s)')";
			Log.d(LOG, query);
			get_database().execSQL(query);

			query = "INSERT INTO " + TableName.unit.toString() + " (`id`, `name`) " + "VALUES (3, 'verse(s)')";
			Log.d(LOG, query);
			get_database().execSQL(query);

		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}


	@Override
	public Exception add() {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			if (_id != -1) {
				values.put("id", _id);
			}//if
			values.put("name", _name);

			get_database().insert(TableName.unit.toString(), null, values);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception update(long old_id) {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			values.put("id", _id);
			values.put("name", _name);

			get_database().update(TableName.unit.toString(), values, "id = ?", new String[] { Long.toString(old_id) });
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception delete() {
		Exception eReturnValue = null;
		try {
			String query = "DELETE FROM " + TableName.unit.toString() + " WHERE id = " + _id;
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception load(long id) {
		Exception returnValue = null;
		try {
			String query = "SELECT * FROM " + TableName.unit.toString() + " WHERE id = " + id;
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
				_id = id;
				_name = cursor.getString(cursor.getColumnIndex("name"));
			}// if
			cursor.close();
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}


}